package com.huasoft.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.huasoft.dao.RightDao;
import com.huasoft.dao.RoleDao;
import com.huasoft.dao.UserDao;
import com.huasoft.domain.Right;
import com.huasoft.domain.Role;
import com.huasoft.domain.User;

public class LoginServlet {

	public void execute(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String loginName = (String) req.getParameter("loginname");
		System.out.println(loginName);
		String password = (String) req.getParameter("password");
		System.out.println(password);
		UserDao userDao = new UserDao();
		User user = userDao.load(loginName, password);
		System.out.println("user---------" + user);
		if (user == null) {
			req.setAttribute("error_login", "用户名或密码错误");
			req.getRequestDispatcher("/login.jsp").forward(req, resp);
		} else {
			HttpSession session = req.getSession();
			session.setAttribute("user", user);
			RoleDao roleDao = new RoleDao();
			System.out.println(user);
			System.out.println(user.getRoleid());
			Role role = roleDao.getByUserId(user.getRoleid());
			RightDao rightDao = new RightDao();
			List<Right> rights = rightDao.getRightsByRoleId(role.getId());
			session.setAttribute("rights", rights);
			System.out.println(rights);
			req.getRequestDispatcher("/frame/main.jsp").forward(req, resp);
		}
	}

}
